package com.example.springBootweb.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springBootweb.service.SysUserService;
import com.example.springBootweb.entity.SysUser;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;



import java.util.List;

/**
* @author L-HN
* @since 2021-04-22
*/
@Slf4j
@Api(tags = {""})
@RestController
@RequestMapping("/sysUser")
public class SysUserController {
    
    @Autowired
    public SysUserService sysUserService;

    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public Object save(@RequestBody SysUser sysUser){
        boolean b = sysUserService.save(sysUser);
        return b;
    }

    @ApiOperation(value = "根据id删除")
    @PostMapping("/delete/{id}")
    public Object delete(@PathVariable("id") Long id){
        boolean b = sysUserService.removeById(id);
        return b;
    }

    @ApiOperation(value = "批量删除")
    @PostMapping("/delete/batch")
    public Object deleteBatch(@RequestBody String ids){
        List<Long> idList = (List<Long>)JSON.parse(ids);
        boolean b = sysUserService.removeByIds(idList);
        return b;
    }

    @ApiOperation(value = "列表（分页）")
    @GetMapping("/list")
    public Object list(Long pageNum, Long pageSize){
        IPage<SysUser> page = sysUserService.page(
                new Page<>(pageNum, pageSize),
                new LambdaQueryWrapper<SysUser>().eq(false, SysUser::getId, 0));
        return page;
    }

    @ApiOperation(value = "详情")
    @GetMapping("/get/{id}")
    public Object get(@PathVariable("id") Long id){
        SysUser sysUser = sysUserService.getById(id);
        return sysUser;
    }

    @ApiOperation(value = "根据id修改")
    @PostMapping("/update/{id}")
    public Object update(@PathVariable("id") Long id, @RequestBody SysUser sysUser){
        sysUser.setId(Integer.valueOf(String.valueOf(id)));
        boolean b = sysUserService.updateById(sysUser);
        return b;
    }

    @ApiOperation(value = "批量修改")
    @PostMapping("/update/batch")
    public Object updateBatch(@RequestBody String sysUsers){
        List<SysUser> sysUserList = (List<SysUser>)JSON.parse(sysUsers);
        boolean b = sysUserService.updateBatchById(sysUserList);
        return b;
    }

}